//
//  BTMainViewController.m
//  BreadTrip
//
//  Created by Vivitickey on 2017/4/19.
//  Copyright © 2017年 test. All rights reserved.
//

#import "BTMainViewController.h"
#import "UIColor+SWAddition.h"
#import "BTSearchView.h"
#import "BTLoginViewController.h"
@interface BTMainViewController ()<UITextFieldDelegate>
{
    UIView *_bgView;
    UITextField *_textField;
}


@property (nonatomic,strong) BTSearchView *searchView;

@property (nonatomic,weak) UIButton *userButton;

@property (nonatomic,weak) UIButton *cancelButton;

@end

@implementation BTMainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationController.navigationBar.barTintColor = [UIColor sw_colorWithR:75 G:188 B:204];
    
    self.view.backgroundColor = [UIColor sw_colorWithR:246 G:244 B:231];
    
    [self setupSearchBar];
    
    [self setupRightUserBarButton];
    
    [self setupSearchView];
    
}



-(void)setupRightUserBarButton{
    
    UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 23, 23)];
    
    _userButton = button;
    
    [button setBackgroundImage:[UIImage imageNamed:@"nav_user_normal"] forState:UIControlStateNormal];
    
    [button setBackgroundImage:[UIImage imageNamed:@"nav_user_highlighted"] forState:UIControlStateHighlighted];
    
    [button addTarget:self action:@selector(clickRightUserBarButton) forControlEvents:UIControlEventTouchUpInside];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:button];
   
    

}

-(void)setupRightCancelBarButton{
    UIButton *cancelButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 30, 23)];
    
    _cancelButton = cancelButton;
    
    cancelButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
  
    cancelButton.titleLabel.font = [UIFont systemFontOfSize:14];
    
    [cancelButton setTitle:@"取消" forState:UIControlStateNormal];
    
    [cancelButton addTarget:self action:@selector(clickCancel) forControlEvents:UIControlEventTouchUpInside];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:cancelButton];
}

-(void)setupSearchBar{
    UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 290, 30)];
    
    
    //设置圆角效果
    bgView.layer.cornerRadius = 14;
    bgView.layer.masksToBounds = YES;
    
    bgView.backgroundColor = [UIColor sw_colorWithR:10 G:171 B:168];
    
    
    
    //输入框
    _textField = [[UITextField alloc] initWithFrame:CGRectMake(15, 0, CGRectGetWidth(bgView.frame) - 10, CGRectGetHeight(bgView.frame))];
    _textField.font = [UIFont systemFontOfSize:13];
    
    //清除按钮
    _textField.clearButtonMode =UITextFieldViewModeWhileEditing;
    
    _textField.delegate = self;
    //键盘属性
    _textField.returnKeyType = UIReturnKeySearch;
    
    //为textField设置属性占位符
    _textField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@" 搜索内容、地点、用户" attributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
    
    _textField.textColor = [UIColor whiteColor];
    
    
    [bgView addSubview:_textField];
    
    self.navigationItem.titleView = bgView;
    
}

//输入框开始编辑
- (void)textFieldDidBeginEditing:(UITextField *)textField {
    
    //_searchView.hidden = NO;
    
    [UIView animateWithDuration:1 animations:^{
        

        _searchView.transform = CGAffineTransformMakeTranslation(0, self.view.frame.size.height);
        
        
        
    }];
    
    self.navigationItem.rightBarButtonItem.tintColor = [UIColor whiteColor];
    
//    _userButton.hidden = YES;
//    
//    _cancelButton.hidden = NO;
    
    [self setupRightCancelBarButton];
}


-(void)clickCancel{
    
    [UIView animateWithDuration:1 animations:^{
        _searchView.transform = CGAffineTransformMakeTranslation(0, - self.view.frame.size.height + 64);
    }];
    
    [_textField resignFirstResponder];
    
//    _userButton.hidden = NO;
//    
//    _cancelButton.hidden = YES;
    
    [self setupRightUserBarButton];
    
}


-(void)clickRightUserBarButton{
    
    //[self.navigationController pushViewController:[[BTLoginViewController alloc]init] animated:YES];
    
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Login" bundle:nil];
    
    // 创建视图控制器
    BTLoginViewController *loginVC = [storyboard instantiateViewControllerWithIdentifier:@"BTLoginViewController"];
    
    
    [self presentViewController:loginVC animated:YES completion:nil];
}

-(void)setupSearchView{
    
    BTSearchView *searchView = [[BTSearchView alloc]initWithFrame:CGRectMake(0, - self.view.frame.size.height, self.view.frame.size.width, self.view.frame.size.height)];
    
    _searchView = searchView;
    
    //searchView.hidden = YES;
    
    [self.view addSubview:searchView];
    
    searchView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
    
    
}



@end
